﻿//using LDT.Tech.BatchTestTools.BLL;
//using LDT.Tech.BatchTestTools.Model;
//using LDT.Tech.MDI.Library.colLinkButton;
using Ledict.ConditionExtract.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


    public partial class colLinkButton_DropDown : Panel
    {
        public colLinkButton_DropDown()
        {
            InitializeComponent();
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            this.BorderStyle = BorderStyle.None;
            base.OnPaint(pe);
        }
        private struct Item
        {
            public string _tag;
            public string _value;
        }       
        public delegate void ControlLinkButtonClick(string value);
        [Description("单击链接按钮事件，返回所点击按钮的value"), Category("行为")]
        public event ControlLinkButtonClick LinkBtnClicked;
        public delegate void ControlLinkButtonDropDownClick(string value);
        [Description("单击链接下拉列表事件，返回所选中下拉列表的value"), Category("行为")]
        public event ControlLinkButtonClick LinkBtnDropDownClicked;

        private List<LinkLabelEx> linklabel_list = new List<LinkLabelEx>();
        private Dictionary<LinkLabelEx, ToolStripDropDown> lt_dict = new Dictionary<LinkLabelEx, ToolStripDropDown>();
        private Dictionary<ToolStripDropDownButton, LinkLabelEx> tbl_dict = new Dictionary<ToolStripDropDownButton, LinkLabelEx>();
        private LinkLabelEx last_LinkLable = null;

        private string _selectValue = string.Empty;
        public string SelectValue
        {
            get { return _selectValue; }
            //set { _selectValue = value; }
        }
        private string _selectText = string.Empty;
        public string SelectText
        {
            get { return _selectText; }
            //set { _selectText = value; }
        }
        private string _selectTag = string.Empty;
        public string SelectTag
        {
            get { return _selectTag; }
            //set { _selectTag = value; }
        }

        private void Refesh()
        {
            for (int i = 0; i < linklabel_list.Count; i++)
            {
                LinkLabelEx ll = linklabel_list[i];
                Graphics graphics = CreateGraphics();
                SizeF sizeF = graphics.MeasureString(ll.Text, this.Font);
                ll.Size = new Size((int)(Math.Ceiling(sizeF.Width)) + 1, (int)(Math.Ceiling(sizeF.Height)));
                if (i == 0)
                {
                    ll.Left = 0;
                }
                else
                {
                    ll.Left = linklabel_list[i - 1].Right;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="value"></param>
        private LinkLabelEx AddItem(string text, string value, string tag)
        {
            LinkLabelEx ll = new LinkLabelEx();
            ll.Text = string.Format("/{0}", text);
            ll.Value = value;
            ll.Tag = tag;
            //或者字符串的像素尺寸
            Graphics graphics = CreateGraphics();
            SizeF sizeF = graphics.MeasureString(ll.Text, this.Font);
            ll.Size = new Size((int)(Math.Ceiling(sizeF.Width)) + 1, (int)(Math.Ceiling(sizeF.Height)));
            //计算控件要显示的位置
            if (linklabel_list.Count == 0)
            {
                ll.Left = 0;
            }
            else
            {
                ll.Left = linklabel_list[linklabel_list.Count - 1].Right;
            }
            //ll.LinkBehavior = LinkBehavior.HoverUnderline;
            ll.Top = (Size.Height - ll.Size.Height) / 2;
            ll.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
            ll.MouseDoubleClick += new MouseEventHandler(this.colLinkButton_DbClick);
            this.Controls.Add(ll);
            linklabel_list.Add(ll);
            return ll;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj">数据源</param>
        /// <param name="textPropertyName">text显示的字段</param>
        /// <param name="valuePropertyName">value显示的字段</param>
        /// <param name="tagPropertyName">tag显示的字段</param>
        public void AddElement(object source, string textPropertyName, string valuePropertyName, string tagPropertyName)
        {
            Type ty = source.GetType();
            if (ty.Name == "DataTable")
            {
                DataTable dt = source as DataTable;
                if (dt.Rows.Count == 0) return;
                if(!dt.Columns.Contains(textPropertyName)) return;
                if (!dt.Columns.Contains(valuePropertyName)) return;
                if (!dt.Columns.Contains(tagPropertyName)) return;

                string text = dt.Rows[0][textPropertyName].ToString();
                string value = dt.Rows[0][valuePropertyName].ToString();
                string tag = dt.Rows[0][tagPropertyName].ToString();
                LinkLabelEx ll = AddItem(text, value, tag);
                ToolStripDropDown dropDown = new ToolStripDropDown();
                foreach (DataRow dr in dt.Rows)
                {
                    ToolStripDropDownButton btn = new ToolStripDropDownButton();
                    btn.Click += new EventHandler(DropDown_Click);
                    btn.ShowDropDownArrow = false;
                    Item item = new Item()
                    {
                        _value = dr[valuePropertyName].ToString(),
                        _tag = dr[tagPropertyName].ToString()
                    };
                    btn.Tag = item;
                    btn.Text = dr[textPropertyName].ToString();
                    dropDown.Items.Add(btn);
                    tbl_dict.Add(btn, ll);
                }
                lt_dict.Add(ll, dropDown);
            }
            else if (ty.Name == "List`1")
            {
                IList lt = source as IList;
                if (lt.Count == 0) return;
                LinkLabelEx ll = null;
                ToolStripDropDown dropDown = new ToolStripDropDown();
                for (int i=0;i< lt.Count;i++)
                {
                    var obj = lt[i];
                    Type obj_t = obj.GetType();
                    var fInfo_text = obj_t.GetField(textPropertyName);
                    if (fInfo_text == null) return;
                    var fInfo_value = obj_t.GetField(valuePropertyName);
                    if (fInfo_value == null) return;
                    var fInfo_tag = obj_t.GetField(tagPropertyName);
                    if (fInfo_tag == null) return;
                    if (i == 0)
                    {                       
                        string text = fInfo_text.GetValue(obj).ToString();
                        string value = fInfo_value.GetValue(obj).ToString();
                        string tag = fInfo_tag.GetValue(obj).ToString();
                        ll = AddItem(text, value, tag);
                    }                   
                    ToolStripDropDownButton btn = new ToolStripDropDownButton();
                    btn.Click += new EventHandler(DropDown_Click);
                    btn.ShowDropDownArrow = false;
                    Item item = new Item()
                    {
                        _value = fInfo_value.GetValue(obj).ToString(),
                        _tag = fInfo_tag.GetValue(obj).ToString()
                    };
                    btn.Tag = item;
                    btn.Text = fInfo_text.GetValue(obj).ToString();
                    dropDown.Items.Add(btn);
                    tbl_dict.Add(btn, ll);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colLinkButton_Click(object sender, EventArgs e)
        {
            if (last_LinkLable != null)
            {
                last_LinkLable.BackColor = Color.Transparent;
            }
            last_LinkLable = sender as LinkLabelEx;
            last_LinkLable.BackColor = Color.Red;
            string text = last_LinkLable.Text.Replace("/", "");
            _selectText = text;
            _selectValue = last_LinkLable.Value;            
            _selectTag = last_LinkLable.Tag.ToString();
            if (LinkBtnClicked != null)
                LinkBtnClicked(_selectValue);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colLinkButton_DbClick(object sender, MouseEventArgs e)
        {
            colLinkButton_Click(sender, e);
            lt_dict[last_LinkLable].Show(this, last_LinkLable.Location.X, last_LinkLable.Location.Y + last_LinkLable.Height);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropDown_Click(object sender, EventArgs e)
        {
            ToolStripDropDownButton btn = sender as ToolStripDropDownButton;
            LinkLabelEx curll = tbl_dict[btn];
            string curllShowValue = curll.Value;
            if (curllShowValue != ((Item)(btn.Tag))._value 
                || (curllShowValue == ((Item)(btn.Tag))._value && curll == linklabel_list[linklabel_list.Count - 1]))
            {
                curll.Text = string.Format("/{0}", btn.Text);
                curll.Value = ((Item)(btn.Tag))._value;
                curll.Tag = ((Item)(btn.Tag))._tag;
                colLinkButton_Click(curll, e);

                for (int i = linklabel_list.Count - 1; i >= 0; i--)
                {
                    LinkLabelEx ll = linklabel_list[i];
                    if (ll.Left > curll.Left)
                    {
                        lt_dict[ll].Dispose();
                        lt_dict.Remove(ll);

                        List<ToolStripDropDownButton> keys = new List<ToolStripDropDownButton>(tbl_dict.Keys);
                        for (int j = tbl_dict.Count - 1; j >= 0; j--)
                        {
                            if(tbl_dict[keys[j]] == ll)
                            {
                                tbl_dict.Remove(keys[j]);
                            }
                        }
                        linklabel_list.Remove(ll);
                        this.Controls.Remove(ll);
                    }
                }

                if (LinkBtnDropDownClicked != null)
                    LinkBtnDropDownClicked(_selectValue);
                Refesh();
            }           
        }
    }

